<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_state extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		// Do something here
		return true;
		
	}
	
	/**
	 * This is intended to load data from database
	 *
	 * @param integer $id identifier of the object loaded
	 * @return boolean
	 */
	
	public function create(){
		extract($_POST);		
		$data=$this->conn->dbCommand("insert into states (state_name,country_id,status) values('".$state_name."','".$country_id."','1')");		
		if($data){
			return array('message'=>'Successfully Created');			
		}else{
			return array('message'=>'Creation Failed');
		}	
	}
	public function update(){
		extract($_POST);
		$data=$this->conn->dbCommand("update states set state_name='".$state_name."',country_id='".$country_id."' where id=".$id);		
		if($data){
			return array('message'=>'Update Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function all(){
		$result=$this->conn->dbMultiple("select * from states ");
		if($result){
			$totalrows = $this->conn->dbSingle('SELECT FOUND_ROWS() as rows;');
		return array('message'=>"All States","info"=>$result,'total'=>$totalrows);
		}
		else
		return array('message'=>'No States');
		
	}
	public function get(){
		extract($_POST);		
		$result=$this->conn->dbSingle("select *from states where id=".$id);
		if($result)
		return array('message'=>"State Info",'info'=>$result);
		else
		return array('message'=>'State Not Found');
	}
	
	
}
	
?>